//
//  ManageDataViewController.m
//  ZYZB
//
//  Created by ENERGY on 2018/7/23.
//  Copyright © 2018年 ENERGY. All rights reserved.
//

#import "ManageDataViewController.h"
#import "SettingArrowTableViewCell.h"
#import "ManageDataSpecificViewController.h"

@interface ManageDataViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong)UITableView * tableView;
@property (nonatomic, strong)NSMutableArray * cellArr;
@property (nonatomic, strong)UIView * footerView;
@end

@implementation ManageDataViewController(networking)
//运营数据
- (void)ManageListData{
    [self showprogressHUD];
    NSString * timeStr = [self currentTimeStr];
    //str
    NSDictionary * dic = @{
                           @"userName":[UserModel sharedInstanced].mobile,
                           @"t":timeStr
                           };
    //sign
    NSDictionary * signDic = @{
                               @"userName":[UserModel sharedInstanced].mobile,
                               @"t":timeStr,
                               @"merchantNo":@"80d709a97af1b8ab"
                               };
    SortMD5Code * md5Code = [[SortMD5Code alloc] init];
    NSString * jsonStr = [md5Code getJsonWithStrDic:dic withSignDic:signDic];
    NSData *postData = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:@"http://120.78.157.132:82/index.php/homepage/index?api=api1006"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    //设置请求方式
    request.HTTPMethod = @"POST";
    //设置请求参数
    request.HTTPBody = postData;
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        [self hiddenProgressHUD];
        NSDictionary *dictionary =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];//data解析为dictionary
        if (!dictionary) {
            NSLog(@"数据缺失");
            return;
        }
        NSString * compareMD5Str = [md5Code encodeStringWithDictionary:dictionary];
        NSString * signResult = [dictionary objectForKey:@"sign"];//接到的json串sign字段
        NSDictionary * jsonData = [md5Code getNSDictionaryData:dictionary];//解析为字典数据
        if ([jsonData[@"code"] isEqualToString:@"000000"]) {
            if ([jsonData[@"body"] count] > 0) {
                if ([compareMD5Str isEqualToString:signResult]) {
                    NSLog(@"成功");
                    [self.cellArr addObjectsFromArray:jsonData[@"body"]];
                    [self.tableView reloadData];
                }else{
                    NSLog(@"失败");
                }
            }else{
                ShowMessage(jsonData[@"msg"]);
            }
        }else{
            ShowMessage(jsonData[@"msg"]);
        }
    }];
}
@end
@implementation ManageDataViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"运营数据";
    self.view.backgroundColor = [UIColor whiteColor];
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    //返回按钮
    UIButton * backBtn = [[UIButton alloc] initWithFrame:CGRectMake(18/WIDTH_6_SCALE, 14/WIDTH_6_SCALE + 24 + 20, 24/WIDTH_6_SCALE, 30/WIDTH_6_SCALE)];
    [backBtn addTarget:self action:@selector(backBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [backBtn setImage:[UIImage imageNamed:@"out"] forState:UIControlStateNormal];
    UIBarButtonItem * leftItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    [self.view addSubview:self.tableView];
    
    [self ManageListData];//运营数据具体数据
}
#pragma mark - init
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - NAVI_HEIGHT - BOTTOM_HEIGHT) style:UITableViewStylePlain];
        _tableView.backgroundColor = [UIColor whiteColor];
        [_tableView registerClass:[SettingArrowTableViewCell class] forCellReuseIdentifier:NSStringFromClass([SettingArrowTableViewCell class])];
        _tableView.rowHeight = 56/WIDTH_6_SCALE;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = self.footerView;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }return _tableView;
}
- (NSMutableArray *)cellArr{
    if (!_cellArr) {
        _cellArr = @[].mutableCopy;
    }return _cellArr;
}
- (UIView *)footerView{
    if (!_footerView) {
        _footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 1/WIDTH_6_SCALE)];
        _footerView.backgroundColor = getColor(grayColor);
    }return _footerView;
}
#pragma mark - tableViewDelegate和tableViewDatasource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SettingArrowTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([SettingArrowTableViewCell class]) forIndexPath:indexPath];
    cell.manageDataModel = self.cellArr[indexPath.row];
    if (self.cellArr.count != 0 || self.cellArr != nil) {
        if (indexPath.row == self.cellArr.count - 1) {
            cell.lineView.hidden = YES;
        }
    }
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.cellArr.count;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    SettingArrowTableViewCell * cell = (SettingArrowTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
//    cell.manageDataModel = self.cellArr[indexPath.row];
    ManageDataSpecificViewController * vc = [[ManageDataSpecificViewController alloc] init];
    vc.navigationItem.title = [self.cellArr[indexPath.row] objectForKey:@"infoTitle"];
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark - action
- (void)backBtnClick:(UIButton *)sender{
    [self.navigationController popViewControllerAnimated:YES];
}
@end
